package com.wtp.wallpaper.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtp.wallpaper.annotation.ReplaceURL;
import com.wtp.wallpaper.pojo.dto.PictureDTO;
import com.wtp.wallpaper.pojo.entity.Picture;
import com.wtp.wallpaper.pojo.query.PageQuery;
import com.wtp.wallpaper.pojo.vo.PictureVO;
import com.wtp.wallpaper.pojo.vo.PicurlVO;
import com.wtp.wallpaper.pojo.result.Result;
import com.wtp.wallpaper.service.PictureService;
import com.wtp.wallpaper.es.service.PictureDocumentService;
import com.wtp.wallpaper.utils.PojoConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController("adminPictureController")
@Slf4j
@RequestMapping("/admin/picture")
public class PictureController {

    @Autowired
    private PictureService pictureService;

    @Autowired
    private PictureDocumentService pictureDocumentService;

    //展示图片 分页查询
    @ReplaceURL
    @GetMapping("/wallPaperPage")
    public Result wallPage(@RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "5") int pageSize,
                           String classId){

        Page<Picture> page = pictureService.lambdaQuery()
                    .eq(classId != null,Picture::getClassId, classId)
                    .page(new PageQuery(pageNum, pageSize)
                            .toMpPageDefaultSortByCreateTime());


        System.out.println(page);
        Page<PictureVO> convert = PojoConvertUtil.convert(page, PictureVO.class);
        return Result.success(convert);
    }

    @ReplaceURL
    @GetMapping("/picturePage")
    public Result picturePage(@RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "5") int pageSize,
                           String classId){

        Page<Picture> page = pictureService.lambdaQuery()
                //.select(Picture::getId,Picture::getSmallpicurl)
                .eq(classId != null,Picture::getClassId, classId)
                .page(new PageQuery(pageNum, pageSize)
                        .toMpPageDefaultSortByCreateTime());


//        System.out.println(page);
        List<PicurlVO> convert = PojoConvertUtil.convert(page.getRecords(), PicurlVO.class);
        return Result.success(convert);
    }

    @PostMapping("/addOne")
    public Result addOnePicture(PictureDTO pictureDTO){
        log.info("传来数据:{}",pictureDTO);
        pictureService.addOne(pictureDTO);
        return Result.success();
    }

    @PostMapping("/modifyOne")
    public Result modifyOnePicture(PictureDTO pictureDTO){
        log.info("传来数据:{}",pictureDTO);
        pictureService.update(pictureDTO);
        return Result.success();
    }

    @PostMapping("/removeByIds")
    public Result removeByIds(PictureDTO pictureDTO){
        log.info("传来ids:{}",pictureDTO);
        pictureService.removePictureByIds(Arrays.stream(pictureDTO.getRemoveIds()).toList());
        //pictureService.removeByIds();
        return Result.success();
    }

}
